package com.xboard.server.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.google.appengine.api.datastore.Query;
import com.xboard.server.dao.UserDao;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserDao userDao;


	@RequestMapping(value="/add", method = RequestMethod.GET)
	public ModelAndView add(HttpServletRequest request, ModelMap model) {
		return new ModelAndView("add");
	}	
	
	@RequestMapping(value="/add/{id}/{name}/{email}", method = RequestMethod.GET)
	public ModelAndView add(@PathVariable String id, @PathVariable String name, @PathVariable String email, 
			HttpServletRequest request, ModelMap model) {
        
		Map<String, Object> user = new HashMap<String, Object>();
		user.put("id", id);
		user.put("name", name);
		user.put("email", email);
		userDao.addUser(user);

		return new ModelAndView("redirect:/user/list");
        
	}
	@RequestMapping(value="/add", method = RequestMethod.POST)
	public ModelAndView addPost(HttpServletRequest request, ModelMap model) {
		
		Map<String, Object> user = new HashMap<String, Object>();
		user.put("id", request.getParameter("id"));
		user.put("name", request.getParameter("name"));
		user.put("email", request.getParameter("email"));
		userDao.addUser(user);
		return new ModelAndView("redirect:/user/list");
	}		
	@RequestMapping(value="/list")
	public ModelAndView listenUser() {

		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();

		list = userDao.listUser();
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("success",true);
		mav.addObject("rows",  list);
	    return mav;
	}

	@SuppressWarnings("deprecation")
	@RequestMapping(value="/select/{id}", method = RequestMethod.GET)
	public ModelAndView getUser(@PathVariable String id,
			HttpServletRequest request, ModelMap model) {

		Map<String, Object> result = userDao.getUser(id);
		
		ModelAndView mav = new ModelAndView("update");
		mav.addObject("success",true);
		mav.addObject("row",  result);
		
		return mav;
	}

	@SuppressWarnings("deprecation")
	@RequestMapping(value="/update/{id}/{name}/{email}", method = RequestMethod.GET)
	public ModelAndView updateUser(@PathVariable String id, @PathVariable String name, @PathVariable String email, 
			HttpServletRequest request, ModelMap model) {

		Map<String, Object> user = new HashMap<String, Object>();
		user.put("id", id);
		user.put("name", name);
		user.put("email", email);
		userDao.updateUser(user);
		
		return new ModelAndView("redirect:/user/list");

	}
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public ModelAndView updateUserPost(HttpServletRequest request, ModelMap model) {

		Map<String, Object> user = new HashMap<String, Object>();
		user.put("id", request.getParameter("id"));
		user.put("name", request.getParameter("name"));
		user.put("email", request.getParameter("email"));
		userDao.updateUser(user);
		
		return new ModelAndView("redirect:/user/list");

	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/delete/{id}", method = RequestMethod.GET)
	public ModelAndView delete(@PathVariable String id,
			HttpServletRequest request, ModelMap model) {
		
		userDao.deleteUser(id);
		return new ModelAndView("redirect:/user/list");
	}
}
